package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.Address;

import com.armani.model.Result;
import com.armani.service.AddressService;
import com.armani.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.text.ParseException;

import java.util.List;

/**
 * @ProjectName: armani
 * @Package: com.armani.controller
 * @ClassName: AddressController
 * @Description: TODO
 * @Author: Roc
 * @Date: 2021/11/30 17:58
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value = "/address",produces = "application/json; charset=utf-8")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping(value = "/list/search.do")
    @ResponseBody
    public String ListSearch(@RequestParam String shopUser,@RequestParam Integer userId,
                             @RequestParam String phone, @RequestParam String province,
                             @RequestParam  String city, @RequestParam String district,
                             @RequestParam  String detail, @RequestParam Integer page,
                             @RequestParam Integer limit) {
        Integer startRow = (page - 1) * limit;
        List<Address> addresses = addressService.ListSearch(shopUser,userId,phone,province,city,
                district,detail,startRow,limit);
        Integer count = addressService.getCount(shopUser,userId,phone,province,city,district,detail);

        Result result = new Result();
        result.setData(addresses);
        result.setCount(count);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/insert.do")
    @ResponseBody
    public String insert(@RequestParam(required = false) String shopUser,
                         @RequestParam(required = false)Integer userId,
                         @RequestParam(required = false)String phone,
                         @RequestParam(required = false)String province,
                         @RequestParam(required = false)String city,
                         @RequestParam(required = false)  String district,
                         @RequestParam(required = false)String detail) throws ParseException {

        addressService.insert(shopUser,userId,phone,province,city,district,detail);

        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/update.do")
    @ResponseBody
    public String update( @RequestParam String shopUser, @RequestParam Integer userId,
                          @RequestParam String phone, @RequestParam  String province,
                          @RequestParam  String city,@RequestParam String district,
                          @RequestParam String detail,@RequestParam Integer id) throws ParseException {

        addressService.update(shopUser,userId,phone,province,city,district,detail,id);

        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer id) {
        addressService.delete(id);
        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    /**
     * 查询用户收货地址
     * @param userId 用户id
     * @return 收货地址列表
     */
    @GetMapping(value = "search/user.do")
    @ResponseBody
    public String searchByUserId(@RequestParam Integer userId){
        List<Address> addresses = addressService.searchByUserId(userId);
        Result result=new Result();
        result.setCode(0);
        result.setData(addresses);
        result.setMsg("成功");
        return JsonUtil.stringify(result);
    }
}
